package com.pn.service.impl;

import com.pn.entity.ProductType;
import com.pn.entity.Result;
import com.pn.mapper.ProductTypeMapper;
import com.pn.service.ProductTypeService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "com.pn.service.impl.ProductTypeServiceImpl")
@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Cacheable(key = "'all:typeTree'")
    @Override
    public List<ProductType> productTypeTree() {

        List<ProductType> allProductTypeList = productTypeMapper.selectAllProductType();

        return allTypeToTypeTree(allProductTypeList, 0);
    }

    @Override
    public Result verifyTypeCodeOrName(String typeCode) {

        ProductType productType = new ProductType();
        productType.setTypeCode(typeCode);

        ProductType info = productTypeMapper.selectProductTypeByCodeOrName(productType);
        return Result.ok(info == null);
    }


    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result saveProductType(ProductType productType) {

        ProductType type = new ProductType();
        type.setTypeName(productType.getTypeName());

        // 校验商品分类名称是否存在
        ProductType info = productTypeMapper.selectProductTypeByCodeOrName(type);
        if (info != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "商品分类编码已存在！");
        }

        int result = productTypeMapper.insertProductType(productType);
        if (result > 0) {
            return Result.ok("商品分类添加成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS, "商品分类添加失败！");
    }

    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result removeProductTypeById(Integer typeId) {

        int result = productTypeMapper.deleteProductTypeById(typeId);
        if (result > 0) {
            return Result.ok("删除商品分类成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "删除商品分类失败！");
    }

    @CacheEvict(key = "'all:typeTree'")
    @Override
    public Result setProductTypeById(ProductType productType) {

        // 查询分类名称是否存在
        ProductType info = new ProductType();
        info.setTypeName(productType.getTypeName());
        ProductType type = productTypeMapper.selectProductTypeByCodeOrName(info);
        if (type != null && !productType.getTypeId().equals(type.getTypeId())) {
            return Result.err(Result.CODE_ERR_BUSINESS, "商品分类名称已存在！");
        }

        int i = productTypeMapper.updateProductById(productType);
        if (i > 0) {
            return Result.ok("商品分类信息修改成功！");
        }

        return Result.err(Result.CODE_ERR_BUSINESS, "商品分类信息修改失败！");
    }

    /**
     * 将所有商品分类转成商品分类树的递归算法
     *
     * @param typeList 所有商品分类
     * @param pid      父级ID
     * @return 商品分类树
     */
    private List<ProductType> allTypeToTypeTree(List<ProductType> typeList, Integer pid) {

        List<ProductType> firstLevelType = new ArrayList<>();

        for (ProductType productType : typeList) {

            if (productType.getParentId().equals(pid)) {
                firstLevelType.add(productType);
            }
        }

        for (ProductType productType : firstLevelType) {
            List<ProductType> secondLevelType = allTypeToTypeTree(typeList, productType.getTypeId());
            productType.setChildProductCategory(secondLevelType);
        }

        return firstLevelType;
    }

}
